package cn.edu.neusoft.zengweidong707.neufood.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsSuccessBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;

public class FoodOrderActivity extends BaseActivity {
    private TextView foodname;
    private TextView foodprice;
    private TextView username;
    private TextView sum;
    private TextView num;
    private Button buy;
    private Button add;
    private Button sub;
    private Spinner spinner;
    private String userName;
    private Double foodPrice;
    private Double finalPrice;
    private TextView time;
    private TextView address;
    private int foodId;
    private int n = 1;
    private int userId;
    private UserModel model;
    private ZwdListener<IsSuccessBean> listener;
    private String suggesttime;
    private String flag;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_food_order);
        foodname = (TextView) findViewById(R.id.foodname);
        foodprice = (TextView) findViewById(R.id.foodprice);
        username = (TextView) findViewById(R.id.username);
        sum = (TextView) findViewById(R.id.sum);
        num = (TextView) findViewById(R.id.num);
        buy = (Button) findViewById(R.id.ok);
        add = (Button) findViewById(R.id.add);
        sub = (Button) findViewById(R.id.sub);
        time = (TextView) findViewById(R.id.time);
        address = (TextView) findViewById(R.id.address);
        spinner = (Spinner) findViewById(R.id.sendtime);
    }

    @Override
    void initEvent() {
        listener = new ZwdListener<IsSuccessBean>() {
            @Override
            public void onResponse(IsSuccessBean l) {
                flag = l.getSuccess();
            }

            @Override
            public void onFail(String msg) {
                showToast("请检查网络是否正常");
            }
        };


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n++;
                num.setText(String.valueOf(n));
                finalPrice = n * foodPrice;
                String num_fp = String.valueOf(finalPrice);
                sum.setText(num_fp);
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( n >=2 )
                {
                    n--;
                    num.setText(String.valueOf(n));
                    finalPrice = n * foodPrice;
                    String num_fp = String.valueOf(finalPrice);
                    sum.setText(num_fp);
                }
                else
                {
                    showToast("数量已达最小");
                }
            }
        });

        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (address.getText().toString().isEmpty()) {
                    showToast("地址不能为空");
                } else {
                    if (flag.equals("1")) {
                        showToast("购买成功");
                        finish();
                    } else {
                        showToast("购买失败");
                    }
                }
            }
        });
    }

    @Override
    void initData() {
        userId = Integer.parseInt(getUserid().toString());
        userName = getUsername();
        n = Integer.parseInt(num.getText().toString());
        suggesttime = spinner.getSelectedItem().toString();
        Bundle bundle = getIntent().getExtras();
        String foodName = bundle.getString("foodname");
        foodPrice = bundle.getDouble("foodprice");
        foodId = bundle.getInt("foodid");
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(d);
        time.setText("下单时间：" + date);

        username.setText(userName);
        foodname.setText(foodName);
        foodprice.setText(foodPrice.toString());
        finalPrice = n * foodPrice;
        String num_fp = String.valueOf(finalPrice);
        sum.setText(num_fp);

        model = new UserModel();
        model.userBuyFood(userId, foodId, n, finalPrice, suggesttime, listener);

    }

}
